package com.ycsj.goldmedalnewconcept.fragment;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.activity.ClassThirdActivity;
import com.ycsj.goldmedalnewconcept.bean.GetCollectResq;
import com.ycsj.goldmedalnewconcept.bean.UserVedioListInfo;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

public class MineShoucangVideoFragment extends Fragment {
	View rootView;
	private ListView lv_favor;
	private RelativeLayout rl_nodata;
	Handler handler = new Handler();

	private ArrayList<UserVedioListInfo> rightNums = new ArrayList<UserVedioListInfo>();
	MyAdapter adapter = new MyAdapter();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_sc_right, null);
		lv_favor = (ListView) rootView.findViewById(R.id.lv_favor);
		getActivity().findViewById(R.id.sv).setVisibility(View.VISIBLE);
		// getActivity().findViewById(R.id.mine_header).setVisibility(View.VISIBLE);

		rl_nodata = (RelativeLayout) rootView.findViewById(R.id.rl_nodata);

		initData();

		initlistener();

		rightNums.removeAll(rightNums);

		return rootView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
		initData();
		super.onResume();
	}
	
	private void initlistener() {
		// TODO Auto-generated method stub
		lv_favor.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Intent intent = new Intent(getActivity(),
						ClassThirdActivity.class);
				intent.putExtra("lESSON_ID", rightNums.get(arg2).VEDIO_ID);
				intent.putExtra("LESSON_NAME", rightNums.get(arg2).LESSON_NAME);
				startActivity(intent);
			}
		});
	}

	private void initData() {
		// TODO Auto-generated method stub
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
		final Request request = new Request.Builder().url(
				NetConstant.USERFAVOR
						+ "/GetUserVedioList.aspx?account="
						+ SPUtils.getString(getActivity(),
								ConfigConstant.USERNAME)
						+ "&usertoken="
						+ SPUtils.getString(getActivity(),
								ConfigConstant.USERTOKEN)).build();
		// new call
		Call call = mOkHttpClient.newCall(request);
		// 请求加入调度
		call.enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResponse(Response response) throws IOException {

				// Toast.makeText(getActivity(), "取消成功", 0).show();
				String json = response.body().string();

				// Log.i("test", json);
				Gson gson = new Gson();
				// 2 使用gson对象的方法
				GetCollectResq getCollectResq = gson.fromJson(json,
						GetCollectResq.class);
				// Log.e("items", getCollectResq.VIDEO_LIST.get(0).toString());
				for (UserVedioListInfo userVedioListInfo : getCollectResq.VIDEO_LIST) {
					if (!"".equals(userVedioListInfo.WEB_URL)) {
						rightNums.add(userVedioListInfo);
					}
					for (int i = 0; i < rightNums.size() - 1; i++) {
						for (int j = rightNums.size() - 1; j > i; j--) {
							if (rightNums.get(j).VEDIO_ID.equals(rightNums
									.get(i).VEDIO_ID)) {
								rightNums.remove(i);
							}
						}
					}
				}
				// rightNums.addAll(getCollectResq.VIDEO_LIST);
				adapter.setInfos(rightNums);
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub

						if (rightNums.size() == 0) {
							
							rl_nodata.setVisibility(rl_nodata.VISIBLE);
							
						} else {
							
							lv_favor.setAdapter(adapter);
							
						}
					}
				}, 0);
			}
		});
	}

	class MyAdapter extends BaseAdapter {

		ArrayList<UserVedioListInfo> adapterNums;

		public void setInfos(ArrayList<UserVedioListInfo> adapterNums) {

			this.adapterNums = adapterNums;
//			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return adapterNums.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHodler viewHodler = null;
			if (convertView == null) {
				// LayoutInflater inflater = (LayoutInflater)
				// MineShoucangActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = View.inflate(getActivity(),
						R.layout.items_video_shoucang, null);
				viewHodler = new ViewHodler();
				viewHodler.tv_sc_grade = (TextView) convertView
						.findViewById(R.id.tv_sc_grade);
				viewHodler.tv_sc_lesson = (TextView) convertView
						.findViewById(R.id.tv_sc_lesson);
				viewHodler.tv_sc_time = (TextView) convertView
						.findViewById(R.id.tv_sc_time);
				viewHodler.iv_video_sc = (ImageView) convertView
						.findViewById(R.id.iv_video_sc);

				convertView.setTag(viewHodler);
			} else {
				viewHodler = (ViewHodler) convertView.getTag();
			}

			viewHodler.tv_sc_grade
					.setText(adapterNums.get(position).GRADE_NAME);
			viewHodler.tv_sc_lesson
					.setText(adapterNums.get(position).LESSON_NAME);
			viewHodler.tv_sc_time.setText(adapterNums.get(position).DATETIME);

			Picasso.with(getActivity()).load(adapterNums.get(position).PIC_URL)
					.fit().into(viewHodler.iv_video_sc);

			return convertView;

		}

		class ViewHodler {
			public TextView tv_sc_grade;
			public TextView tv_sc_lesson;
			public TextView tv_sc_time;
			public ImageView iv_video_sc;
		}
	}
}